<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 掌握的单词个数（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>有一个字符串数组 words 和一个字符串 chars。</p> 
<p>假如可以用 chars 中的字母拼写出 words 中的某个“单词”&#xff08;字符串&#xff09;&#xff0c;那么我们就认为你掌握了这个单词。</p> 
<p>words 的字符仅由 a-z 英文小写字母组成&#xff0c;例如 &#34;abc&#34;</p> 
<p>chars 由 a-z 英文小写字母和 &#34;?&#34; 组成。其中英文 &#34;?&#34; 表示万能字符&#xff0c;能够在拼写时当作任意一个英文字母。例如&#xff1a;&#34;?&#34; 可以当作 &#34;a&#34; 等字母。</p> 
<p>注意&#xff1a;每次拼写时&#xff0c;chars 中的每个字母和万能字符都只能使用一次。</p> 
<p>输出词汇表 words 中你掌握的所有单词的个数。没有掌握任何单词&#xff0c;则输出0。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行&#xff1a;输入数组 words 的个数&#xff0c;记作N。</p> 
<p>第二行 ~ 第N&#43;1行&#xff1a;依次输入数组words的每个字符串元素</p> 
<p>第N&#43;2行&#xff1a;输入字符串chars</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出一个整数&#xff0c;表示词汇表 words 中你掌握的单词个数</p> 
<p></p> 
<h4>备注</h4> 
<ul><li>1 ≤ words.length ≤ 100</li><li>1 ≤ words[i].length, chars.length ≤ 100</li><li>所有字符串中都仅包含小写英文字母、英文问号</li></ul> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">4<br /> cat<br /> bt<br /> hat<br /> tree<br /> atach??</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">可以拼写字符串&#34;cat&#34;、&#34;bt&#34;和&#34;hat&#34;</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3<br /> hello<br /> world<br /> cloud<br /> welldonehohneyr</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">可以拼写字符串&#34;hello&#34;和&#34;world&#34;</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3<br /> apple<br /> car<br /> window<br /> welldoneapplec?</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">可以拼写字符串&#34;apple&#34;和“car”</td></tr></tbody></table> 
<p> </p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以分别统计出chars和word中各字符的数量&#xff0c;然后遍历word每个字符c&#xff0c;比较word和chars中统计的c字符数量&#xff0c;如果word的c数量超过了chars的c数量&#xff0c;那么就就将超出数量计入diff中。</p> 
<p>最终比较diff和chars中万能字符‘?’的数量&#xff0c;如果chars中万能字符‘?’的数量 &gt;&#61; diff&#xff0c;那么说明chars可以使用万能字符补足不同部分&#xff0c;即可以学会word。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const n &#61; parseInt(await readline());

  const words &#61; [];
  for (let i &#61; 0; i &lt; n; i&#43;&#43;) words.push(await readline());

  const chars &#61; await readline();

  console.log(getResult(words, chars));
})();

function getResult(words, chars) {
  let ans &#61; 0;

  // 统计chars字符串中各字符的数量
  const cnt_chars &#61; charStatistic(chars);

  for (let word of words) {
    let diff &#61; 0;

    // 统计word字符串中各字符的数量
    const cnt_word &#61; charStatistic(word);

    for (let j &#61; 0; j &lt; 128; j&#43;&#43;) {
      // 记录word的某字符超过chars的对应字符出现的数量
      diff &#43;&#61; Math.max(cnt_word[j] - cnt_chars[j], 0);
    }

    if (diff &lt;&#61; cnt_chars[&#34;?&#34;.charCodeAt()]) {
      ans&#43;&#43;;
      // console.log(word);
    }
  }

  return ans;
}

function charStatistic(s) {
  const cnts &#61; new Array(128).fill(0);

  for (let c of s) {
    cnts[c.charCodeAt()]&#43;&#43;;
  }

  return cnts;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    int n &#61; sc.nextInt();

    String[] words &#61; new String[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      words[i] &#61; sc.next();
    }

    String chars &#61; sc.next();

    System.out.println(getResult(words, n, chars));
  }

  public static int getResult(String[] words, int n, String chars) {
    int ans &#61; 0;

    // 统计chars字符串中各字符的数量
    int[] cnt_chars &#61; charStatistic(chars);

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      int diff &#61; 0;

      // 统计word字符串中各字符的数量
      int[] cnt_word &#61; charStatistic(words[i]);

      for (int j &#61; 0; j &lt; 128; j&#43;&#43;) {
        // 记录word的某字符超过chars的对应字符出现的数量
        diff &#43;&#61; Math.max(cnt_word[j] - cnt_chars[j], 0);
      }

      if (diff &lt;&#61; cnt_chars[&#39;?&#39;]) {
        ans&#43;&#43;;
        // System.out.println(words[i]);
      }
    }

    return ans;
  }

  public static int[] charStatistic(String s) {
    int[] cnts &#61; new int[128];

    for (int i &#61; 0; i &lt; s.length(); i&#43;&#43;) {
      char c &#61; s.charAt(i);
      cnts[c] &#43;&#61; 1;
    }

    return cnts;
  }
}
</code></pre> 
<p> </p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())

words &#61; []
for i in range(n):
    words.append(input())

chars &#61; input()


def charStatistic(s):
    cnts &#61; [0] * 128

    for c in s:
        cnts[ord(c)] &#43;&#61; 1

    return cnts


# 算法入口
def getResult():
    ans &#61; 0

    # 统计chars字符串中各字符的数量
    cnt_chars &#61; charStatistic(chars)

    for word in words:
        diff &#61; 0

        # 统计word字符串中各字符的数量
        cnt_word &#61; charStatistic(word)

        for j in range(128):
            # 记录word的某字符超过chars的对应字符出现的数量
            diff &#43;&#61; max(cnt_word[j] - cnt_chars[j], 0)

        if diff &lt;&#61; cnt_chars[ord(&#39;?&#39;)]:
            ans &#43;&#61; 1
            # print(word)

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define MAX(a,b) ((a) &gt; (b) ? (a) : (b))

int* charStatistic(const char* s) {
    int* cnts &#61; (int*) calloc(128, sizeof(int));

    int i &#61; 0;
    while(s[i] !&#61; &#39;\0&#39;) {
        cnts[s[i]]&#43;&#43;;
        i&#43;&#43;;
    }

    return cnts;
}

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    char words[100][101];

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
        scanf(&#34;%s&#34;, words[i]);
    }

    char chars[101];
    scanf(&#34;%s&#34;, chars);

    // 记录题解
    int ans &#61; 0;

    // 统计chars字符串中各字符的数量
    int* cnts_char &#61; charStatistic(chars);

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        int diff &#61; 0;

        // 统计word字符串中各字符的数量
        int* cnt_word &#61; charStatistic(words[i]);

        for(int j&#61;0; j&lt;128; j&#43;&#43;) {
            // 记录word的某字符超过chars的对应字符出现的数量
            diff &#43;&#61; MAX(cnt_word[j] - cnts_char[j], 0);
        }

        if(diff &lt;&#61; cnts_char[&#39;?&#39;]) {
            ans&#43;&#43;;
            // puts(words[i]);
        }
    }

    printf(&#34;%d\n&#34;, ans);
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>